﻿using Newtonsoft.Json.Linq;
using pixeleditor.WorkingSpace.Resource;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ttbit.core;

namespace ttbit.WorkingSpace
{

    class Parser_Sprite : IWorkResourceParser
    {
        readonly string[] __exts = new string[] { ".jpg", ".png"};
        public string[] FileExt => __exts;

        public string TypeName => Resource_Sprite.TypeStr;

        public override string ToString()
        {
            return TypeName + "=>" + __exts[0];
        }
        public bool CanCreate => false;
        public IWorkResource Load(bool reimport, System.IO.Stream metaStream, string path)
        {
            var name = Path.GetFileNameWithoutExtension(path).ToLower();
            var res = new Resource_Sprite(name);
            try
            {
                var binreader = new System.IO.BinaryReader(metaStream);
                var line = binreader.ReadString();
                JObject json = JObject.Parse(line);
                res.sprite.ReadFromJson(json);
            }
            catch (Exception err)
            {
                reimport = true;
                //stream load error
            }

            if (reimport)
            {
                var ext = System.IO.Path.GetExtension(path).ToLower();
                //if (ext == ".spriteref")
                //{
                //    res.sprite.inPack = true;
                //    using var fs = System.IO.File.OpenRead(path);
                //    res.sprite.Read(fs);
                //    return res;
                //}
                //else
                {
                    //res.sprite.inPack = false;
                    var fs = System.IO.File.OpenRead(path);
                    var t = CoreLoadTool.LoadTextureData_RGBA(fs, out int w, out int h);

                    CoreLoadTool.GetBorder(t, w, h, out int x1, out int y1, out int x2, out int y2);

                    res.textureStandalone = new Texture((ushort)w, (ushort)h, IGL.TextureType.RGBA_32, t);
                    res.sprite.uv = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
                    res.sprite.povit.X = (x2-x1) / 2;
                    res.sprite.povit.Y = (y2-y1) / 2;
                    return res;
                }
            }


           // if (!res.sprite.inPack)
            {
                using (var fs = System.IO.File.OpenRead(path))
                {
                    var t = CoreLoadTool.LoadTextureData_RGBA(fs, out int w, out int h);
                    res.textureStandalone = new Texture((ushort)w, (ushort)h, IGL.TextureType.RGBA_32, t);
                }
            }
            return res;
        }
        public bool Save(IWorkResource resource, System.IO.Stream metaStream, string path)
        {
            var res = resource as Resource_Sprite;

            //if (res.sprite.inPack)
            //{//InPack Sprite 需要写一个占位文件
            //    using var fs = System.IO.File.OpenWrite(path);
            //    res.sprite.Write(fs);
            //}
            //write to meta.

            var binwrite = new System.IO.BinaryWriter(metaStream);
            {
                JObject json = new JObject();
                res.sprite.WriteToJson(json);
                var jsonstr = json.ToString(Newtonsoft.Json.Formatting.None) + "\n";
                binwrite.Write(jsonstr);
            }


            //no need to write to data;
            return true;
        }


    }
}
